package cn.yuanqiao.web.controller.manage;

import cn.yuanqiao.common.constant.SysContants;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.service.SysPermissionService;
import cn.yuanqiao.manage.domain.ManageUser;
import cn.yuanqiao.manage.service.IManageUserService;

import cn.yuanqiao.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 管理员信息Controller
 * 
 * @author yuanqiao
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/manage/user")
public class ManageUserController extends BaseController
{
    @Autowired
    private IManageUserService manageUserService;
    @Autowired
    private ISysUserService remoteUserService;
    @Value("${encryption}")
    private String encryption;

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 查询管理员信息列表
     */
    //@RequiresPermissionsmissions("manage:user:list")
    @Log(title = "查询管理员信息列表",businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(ManageUser manageUser)
    {
        startPage();
        List<ManageUser> list = manageUserService.selectManageUserList(manageUser);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    /**
     * 导出管理员信息列表
     */
    //@RequiresPermissionsmissions("manage:user:export")
    //@Log(title = "管理员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ManageUser manageUser)
    {
        List<ManageUser> list = manageUserService.selectManageUserList(manageUser);
        ExcelUtil<ManageUser> util = new ExcelUtil<ManageUser>(ManageUser.class);
        util.exportExcel(response, list, "管理员信息数据");
    }

    /**
     * 获取管理员信息详细信息
     */
    //@RequiresPermissionsmissions("manage:user:query")
    @Log(title = "获取管理员信息详细信息",businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        ManageUser manageUser = manageUserService.selectManageUserByID(ID);
        return success(manageUser);
    }

    /**
     * 新增管理员信息
     */
    //@RequiresPermissionsmissions("manage:user:add")
    @Log(title = "新增管理员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ManageUser manageUser)
    {
        return toAjax(manageUserService.insertManageUser(manageUser));
    }

    /**
     * 修改管理员信息
     */
    //@RequiresPermissionsmissions("manage:user:update")
    @Log(title = "修改管理员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ManageUser manageUser)
    {
        return toAjax(manageUserService.updateManageUser(manageUser));
    }

    /**
     * 删除管理员信息
     */
    //@RequiresPermissionsmissions("manage:user:delete")
    @Log(title = "删除管理员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(manageUserService.deleteManageUserByIDs(IDs));
    }

    @PostMapping("/resetUserPwd/{loginName}/{passWord}")
    public R<String> resetUserPwd(@PathVariable("loginName") String loginName, @PathVariable("passWord") String passWord){
        int i = manageUserService.resetUserPwd(loginName, SecurityUtils.encryptPassword(passWord,encryption));
        return i>0? R.ok("success"): R.fail("error");
    }

    @GetMapping("/info/{loginName}")
    public R<LoginUser> info(@PathVariable("loginName") String loginName)
    {
        ManageUser manageUser = manageUserService.selectUserByLoginName(loginName);
        if (StringUtils.isNull(manageUser))
        {
            return R.fail("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = new HashSet<>();
        roles.add(SysContants.MANAGEROLEFLAG);
        // 权限集合
        Set<String> permissions = sysPermissionService.selectMenuPersByRoleId(SysContants.MANAGEROLEFLAG);
        SysUser sysUser = new SysUser();
        sysUser.setDelFlag("0");
        sysUser.setUserId(manageUser.getId());
        sysUser.setPassword(manageUser.getPassWord());
        sysUser.setStatus("0");
        sysUser.setUserName(manageUser.getLoginName());
        sysUser.setNickName(manageUser.getUserName());
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setUser(sysUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        return R.ok(sysUserVo);
    }
}
